package org.example.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置类
 */
@Configuration
public class TtlQueueConfig {
    //普通交换机名称
    public static final String X_EXCHANGE = "XE";
    //死信交换机名称
    public static final String D_EXCHANGE = "DE";
    //普通队列名称
    public static final String Q1_QUEUE = "QU1";
    public static final String Q2_QUEUE = "QU2";
    public static final String Q3_QUEUE = "QU3";
    //私信队列名称
    public static final String D_QUEUE = "DQ";

    //声明交换机,这里也可以不用起别名,默认是方法名
    @Bean("XExchange")
    public DirectExchange XExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    @Bean("DExchange")
    public DirectExchange DExchange(){
        return new DirectExchange(D_EXCHANGE);
    }

    //声明普通队列 TTL为10s
    @Bean("Queue1")
    public Queue Queue1(){
        Map<String,Object> arguments = new HashMap<>(4);
        //设置过期后的死信交换机
        arguments.put("x-dead-letter-exchange",D_EXCHANGE);
        //设置死信的RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置TTL
        arguments.put("x-message-ttl", 10 * 1000);
        return QueueBuilder.durable(Q1_QUEUE).withArguments(arguments).build();
    }

    @Bean("Queue2")
    public Queue Queue2(){
        Map<String,Object> arguments = new HashMap<>(4);
        //设置过期后的死信交换机
        arguments.put("x-dead-letter-exchange",D_EXCHANGE);
        //设置死信的RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置TTL
        arguments.put("x-message-ttl", 40 * 1000);
        return QueueBuilder.durable(Q2_QUEUE).withArguments(arguments).build();
    }

    // Q3不设置过期时间,过期时间由生产者控制
    @Bean()
    public Queue Queue3(){
        Map<String,Object> arguments = new HashMap<>(4);
        //设置过期后的死信交换机
        arguments.put("x-dead-letter-exchange",D_EXCHANGE);
        //设置死信的RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        return QueueBuilder.durable(Q3_QUEUE).withArguments(arguments).build();
    }

    @Bean("DQueue")
    public Queue DQueue(){
        return QueueBuilder.durable(D_QUEUE).build();
    }

    @Bean()
    public Binding QU1BindingXE(@Qualifier("Queue1") Queue queue1,
                                @Qualifier("XExchange") Exchange xExchange){
        return BindingBuilder.bind(queue1).to(xExchange).with("XQ1").noargs();
    }

    @Bean()
    public Binding QU2BindingDE(@Qualifier("Queue2") Queue queue2,
                                @Qualifier("XExchange") Exchange xExchange){
        return BindingBuilder.bind(queue2).to(xExchange).with("XQ2").noargs();
    }

    @Bean()
    public Binding QU3BindingDE(@Qualifier("Queue3") Queue queue3,
                                @Qualifier("XExchange") Exchange xExchange){
        return BindingBuilder.bind(queue3).to(xExchange).with("XQ3").noargs();
    }

    @Bean()
    public Binding DQBindingXE(@Qualifier("DQueue") Queue dQueue,
                                @Qualifier("DExchange") Exchange dExchange){
        return BindingBuilder.bind(dQueue).to(dExchange).with("YD").noargs();
    }

}
